<?php
require_once 'Zend/Loader.php';
require_once 'Zend/Registry.php';
require_once 'application/library/config/gcConfigRegistry.php';
require_once 'application/library/helper/jsonHelper.php';
require_once 'application/library/helper/configHelper.php';
require_once 'application/library/helper/sessionHelper.php';
require_once 'application/library/helper/userDataHelper.php';
require_once 'application/library/view/gcViewSmarty.php';
require_once 'application/library/validate/gcBusinessValidate.php';
require_once 'application/library/validate/gcStandardValidate.php';
require_once 'application/constant/constant.php';

Zend_Loader::loadClass('Zend_Controller_Action' );

/** 
 * gcBasicAction
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class  gcBasicAction extends Zend_Controller_Action{

    /**
     * view 
     */
    protected $gcView;

    /**
     * validate
     */
    protected $standardValidate;

    /**
     * businessValidate
     */
    protected $businessValidate;
    
    /**
     * translate
     */
    protected $gcTranslate;
    
    /**
     * 初期化
     * init
     */
    public function init(){

        $this->loaderTranslate();

        $this->loaderView();

        $this->loaderValidate();

        $this->setSysParam();

        $this->setHeaderData();
    }

    /**
     * 读取视图
     * loaderView
     */
    private  function loaderView(){
        $ModuleName=$this->getRequest()->getModuleName();
        $templatPath=sysConstant::MODULES_PATH.$ModuleName.sysConstant::VIEWS_DIRECTORY;
        $this->gcView=new gcViewSmarty($templatPath);
    }

    /**
     * 后台验证读取
     * loaderValidate
     * @return
     */
    private function loaderValidate(){
        $this->standardValidate=new gcStandardValidate();
        $this->businessValidate=new gcBusinessValidate();
    }

    /**
     * 国际化
     * loaderTranslate
     */
    private function loaderTranslate(){
        $moduleName=$this->getRequest()->getModuleName();
        $localLanguage=configHelper::getLocalLanguage();
        $options = array('adapter' => "tmx",
                         'content' => sysConstant::MODULES_PATH . $moduleName . sysConstant::LANGUAGES_DIRECTORY,
                         'locale'  => $localLanguage);
        $translate=new Zend_Translate($options);
        $translate =unserialize(gcConfigRegistry::get(sysConstant::SYS_TRANSLATE));

        $translate->addTranslation($options);
        $this->gcTranslate=$translate->getAdapter();
    }

    /**
     * 画面用系统信息设定
     * setSysParam
     */
    private  function setSysParam(){
        $host           = configHelper::getHostServer();
        $pictureHost    = configHelper::getPictureServer();
        $theme          = sessionHelper::getTheme();
        $localLanguage  = configHelper::getLocalLanguage();
        $this->gcView->host=$host;
        $this->gcView->localLanguage=$localLanguage;
        $this->gcView->pictureHost=$pictureHost;
        $this->gcView->theme =$theme;
    }

    /**
     * js用错误提示语句设定
     * setMessageForJs
     * @param   array   $msgId
     */
    protected  function setMessageForJs($msgId){

        $messages=array();
        for($i=0;$i<count($msgId);$i++){
            $message["MessageID"]=$msgId[$i];
            $message["MessageValue"]=html_entity_decode($this->gcTranslate->translate($msgId[$i]));
            $messages[$i]=$message;
        }

        $this->gcView->messages=$messages;
    }

    /**
     * 画面错误信息设定
     * setErrorMsg
     * @param   array   $arrayMsg
     */
    protected function setErrorMsg($arrayMsg) {
        $this->gcView->arrMsg=$arrayMsg;
    }

    /**
     * js用标签设定
     * setLabelForJs
     * @param   array   $labelId
     */
    protected  function setLabelForJs($labelId){

        $labels=array();
        for($i=0;$i<count($labelId);$i++){
            $label["LabelID"]=$labelId[$i];
            $label["LabelValue"]=html_entity_decode( $this->gcTranslate->translate($labelId[$i]));
            $labels[$i]=$label;
        }
        $this->gcView->labels=$labels;
    }

    /**
     * 标签取得
     * getLabel
     * @param   string  $labelId
     * @param   string  $strLabe
     */
    protected function getLabel($labelId){
        $strLabe = $this->gcTranslate->translate($labelId);
        return $strLabe;
    }

    /**
     * 消息取得
     * getMessage
     * @param   string  $messageId
     */
    protected function getMessage($messageId){
        $message = $this->gcTranslate->translate($messageId);
         return $message;
    }
    
    /**
     * 参数取得
     * getParam
     * @param   string  $id
     * @param   boolen  $isJson
     * @return  array   $param
     */
    protected function getParam($id, $isJson=false){
        $param=$this->getRequest()->getParam($id);

        if($isJson && $param !=""){
            $param=str_replace("&quot;", "\"", $param);
            $param=json_decode($param);
            $tempParam=array();
            for($i=0;$i<count($param);$i++){
                $tempParam[$i]=str_replace("\"","&quot;", $param[$i]);
            }
            $param = $tempParam;
        }else {}
        return $param;
    }

    /**
     * 上传文件
     * getUploadObjParam
     * @param   array   $ctlName
     * @return  array
     */
    protected function getUploadObjParam($ctlName) {
        
        $arrUploadIf = array( "status"     =>"",
                              "name"       =>"",
                              "type"       =>"",
                              "size"       =>"",
                              "message"  =>""
                             );
        $errorCode=$_FILES[$ctlName]["error"];

                         var_dump($_FILES);
                exit();
        if ($errorCode > UPLOAD_ERR_OK ){
            if($errorCode == UPLOAD_ERR_PARTIAL){
                $arrUploadIf["status"] = "1";
                $arrUploadIf["message"] = "Fail";
            }

            if($errorCode == UPLOAD_ERR_INI_SIZE ||$errorCode == UPLOAD_ERR_FORM_SIZE) {
                $arrUploadIf["status"] = "2";
                $arrUploadIf["message"] = "Too big";
            }

            if($errorCode == UPLOAD_ERR_NO_FILE ) {
                $arrUploadIf["status"] = "3";
                $arrUploadIf["messageId"] = "No file";
            }
        }else {
            if($_FILES[$ctlName]["size"] == 0){
                $arrUploadIf["status"] = "3";
                $arrUploadIf["messageId"] = "No file";
            }else {
                $arrUploadIf["status"] = "0";
                $arrUploadIf["name"]   = $_FILES[$ctlName]["name"];
                $arrUploadIf["type"]   = $_FILES[$ctlName]["type"];
                $arrUploadIf["size"]   = $_FILES[$ctlName]["size"];
            }
        }
        return $arrUploadIf;
    }
    
    /**
     * 迁移
     * forward
     * @param   string  $id
     * @param   array   $array
     */
    protected function forward($id, $array=null){
        $url=explode("/",$id);
        $this->_forward($url[2],$url[1],$url[0],$array);
    }

    /**
     * 跳转
     * redirect
     * @param string $id
     */
    protected function redirect($id){
        $this->_redirect(configHelper::getHostServer()."/".$id);
    }

    /**
     * 外部URL迁移
     * redirectUrl
     * @param string $url
     */
    protected function redirectUrl($url){
        $this->_redirect($url);
    }

    
    /**
     * 文件下载
     * download
     *@param    string   $srcFile  下载的文件
     *@param    string   $fileName 文件名
     *@return   boolean
    */
    protected function download($srcFile, $fileName = null){
        //$srcFile = iconv("UTF-8", "shift-JIS",$srcFile);
        if(file_exists($srcFile)){
            $arrTemp = array();
            $arrTemp = explode("/",$srcFile);
            if($fileName){
                $arrTemp = explode(".",$arrTemp[sizeof($arrTemp)-1]);
                $fileName = $fileName. "." .$arrTemp[sizeof($arrTemp)-1];
            }else{
                //$fileName = iconv("shift-JIS", "UTF-8",$arrTemp[sizeof($arrTemp)-1]);
                $fileName = $arrTemp[sizeof($arrTemp)-1];
            }
            $Agent = $_SERVER['HTTP_USER_AGENT'];
            $encoded_filename = urlencode(basename($fileName));
            $encoded_filename = str_replace("+", "%20", $encoded_filename);
            if(ereg('Mozilla', $Agent) && ereg('Firefox', $Agent)) {
                $disposition = 'attachment; filename*="utf8\'\'' . $encoded_filename . '"';
            } else {
                $disposition = 'attachment; filename="' . $encoded_filename . '"';
            }
            $this->getResponse()
                ->clearHeaders()
                ->setHeader('Content-Type', 'application/octet-stream; charset=UTF-8')
                ->setHeader('Content-Disposition', $disposition)
                ->setHeader('Content-Transfer-Encoding', 'Binary')
                ->setHeader('Content-Length', filesize($srcFile))
                ->sendHeaders();
            readfile($srcFile);
            return true;
        }else{
            return false;
        }
    }

    /**
     * Header文字设定
     * setHeaderData
     */
    protected function setHeaderData(){
        $this->gcView->passwordEdit = $this->gcTranslate->translate("passwordEdit");
        $this->gcView->logout       = $this->gcTranslate->translate("logout");
        $this->gcView->topDept      = userDataHelper::getDepartment();
        $this->gcView->topName      = userDataHelper::getLoginUserName();
        $this->gcView->topLabel     = $this->gcTranslate->translate("topLabel");
        $this->gcView->goBack       = $this->gcTranslate->translate("goBack");
    }

    /**
     * QRCodeURL设定
     * setPageUrlForQRCode
     */
    protected function setPageUrlForQRCode(){
        $host           = configHelper::getHostServer();
        $moduleName     = $this->getRequest()->getModuleName();
        $controllerName = $this->getRequest()->getControllerName();
        $actionName     = $this->getRequest()->getActionName();
        $this->gcView->pleaseScan = $this->gcTranslate->translate("pleaseScan");
        $this->gcView->strUrlForQRCode = $host ."default/qrcode/index/?d=".$host . $moduleName."/".$controllerName."/".$actionName."/";
    }
}
